package com.demo.project_template.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.project_template.entity.PostComment;
import com.demo.project_template.mapper.PostMapper;
import com.demo.project_template.service.PostCommentService;
import com.demo.project_template.vo.response.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 帖子评论 前端控制器
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-08-20 11:48:57
 */
@Api(tags = "动态（帖子）评论")
@RestController
@RequestMapping("/postComment")
public class PostCommentController {
    @Resource
    private PostCommentService postCommentService;
    @Resource
    private PostMapper postMapper;

    @ApiOperation(value = "分页查询帖子评论")
    @GetMapping(value = "/page")
    public R<IPage<PostComment>> page(@RequestParam Integer pageNo,
                                      @RequestParam Integer pageSize,
                                      @RequestParam Long postId){
        Page<PostComment> result = postCommentService.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<PostComment>()
                .eq(PostComment::getPostId, postId)
                .eq(PostComment::getPid,0L));
        //浏览量+1
        postMapper.addViewCount(postId);
        return R.success(result);
    }

    @ApiOperation(value = "查询某条评论的子评论")
    @GetMapping(value = "/listChild")
    public R<List<PostComment>> listChildComments(@RequestParam Long commentPid){
        return R.success(postCommentService.listChildComments(commentPid));
    }

    @ApiOperation(value = "添加评论")
    @PostMapping(value = "/add")
    public R<?> add(@RequestBody PostComment postComment){
        postCommentService.add(postComment);
        return R.success();
    }

    @ApiOperation(value = "删除评论")
    @DeleteMapping(value = "/id")
    public R<?> delete(@RequestParam Long id){
        postCommentService.removeById(id);
        return R.success();
    }
}
